package cs2340.klondike.TaskFudger;

import java.sql.SQLException;
import java.util.List;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
import com.j256.ormlite.dao.GenericRawResults;
import com.j256.ormlite.stmt.SelectArg;

import db.DatabaseHelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.widget.*;
import android.widget.TextView.*;
import android.util.Log;
import android.view.View;
import models.*;

@SuppressWarnings("unused")
/**
 * UserLoginActivity represents the View for the login screen at the startup of the app
 * the View has two text fields for username and password, a button to login, and a button to create a new account
 * 
 * @author Katherine
 *
 */
public class UserLoginActivity extends OrmLiteBaseActivity<DatabaseHelper> {
	private EditText handleEntry;//text entry field for the username
	private EditText passwordEntry;//text entry field for the password
	private Button loginButton;//button to login the user with the given username and password
	private Button newUserButton;//button to register that a new user account needs to be created
	private TextView errorMessage;
	
	/**
	 * called when the activity is started
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_login);
        
		handleEntry = (EditText)findViewById(R.id.handle);
		passwordEntry = (EditText)findViewById(R.id.password);
		loginButton = (Button)findViewById(R.id.login);
		newUserButton = (Button)findViewById(R.id.new_user);
		errorMessage = (TextView)findViewById(R.id.errorMessage);
		
		if(User.getCurrentUser() != null){
			handleEntry.setText(User.getCurrentUser().getHandle());
			passwordEntry.setText(User.getCurrentUser().getPassword());
		}
        
		errorMessage.setVisibility(View.GONE);
		
		loginButton.setOnClickListener(new Button.OnClickListener() {

			/**
        	 * when the button is clicked, the User with the given password is found
        	 * if the User exists, it is logged in; if not, an error message is sent
        	 * @see android.view.View.OnClickListener#onClick(android.view.View)
        	 */
			public void onClick (View v){
				boolean userExists = false;
				
				try {
					SelectArg handleArg = new SelectArg(handleEntry.getText().toString());
					SelectArg passwordArg = new SelectArg(passwordEntry.getText().toString());
					
					User user =
						User.getDefaultDao().queryForFirst(
							User.getDefaultDao().queryBuilder().where()
								.eq("handle", handleArg)
								.and()
								.eq("password", passwordArg)
								.prepare());
					
					userExists = user != null;
					
					if (userExists)
						Log.i("MATCHINGUSERS", user.toString());
					
					List<User> use = User.getDefaultDao().queryForAll();
					Log.i("POSSIBLEUSERS", use.toString());
					
					
					if (userExists) {
						User.setCurrentUser(user);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
        		if (!userExists){
        			errorMessage.setVisibility(View.VISIBLE);
        			passwordEntry.setText("");
        		}
        		else{
        			Intent intent = new Intent(v.getContext(), TabsActivity.class); //Task List
        			startActivity(intent);
        		}
			}
		});
        
        newUserButton.setOnClickListener(new Button.OnClickListener(){
        	public void onClick(View v){
        		Intent intent = new Intent("cs2340.klondike.TaskFudger.NEWUSER"); //UserLoginActivity
        		startActivity(intent);
        	}
        });
	}
}
